package com.nervenets.shopping.controller.shopping.params;

import com.nervenets.general.exception.LogicException;
import com.nervenets.general.web.params.FormParams;
import com.nervenets.shopping.hibernate.domain.shopping.Product;
import com.nervenets.shopping.service.ProductService;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class ProductFormParams extends FormParams<Product, ProductService> {
    @ApiModelProperty(value = "商品名称", required = true)
    @NotBlank
    private String name;
    @ApiModelProperty(value = "封面图", required = true)
    @NotBlank
    private String coverImage;
    @ApiModelProperty(value = "分享封面图")
    private String shareCoverImage;
    @ApiModelProperty(value = "轮播图")
    private List<String> pictures;
    @ApiModelProperty(value = "价格", required = true)
    @PositiveOrZero
    private int price;
    @ApiModelProperty(value = "原价格", required = true)
    @PositiveOrZero
    private int originalPrice;
    @ApiModelProperty(value = "结算价格", required = true)
    @PositiveOrZero
    private int settlePrice;
    @ApiModelProperty(value = "排序")
    private int queue;
    @ApiModelProperty(value = "是否启用")
    private boolean enable = true;
    @ApiModelProperty(value = "详情描述")
    private String detail;
    @ApiModelProperty(value = "基础销售数量")
    private long baseSaleCount;
    @ApiModelProperty(value = "成团人数", required = true)
    @Positive
    private int groupBuySuccess = 20;
    @ApiModelProperty(value = "成功中签人数", required = true)
    @Positive
    private int groupBuySuccessUser = 3;
    @ApiModelProperty(value = "拼团失败奖励金额，0.3元-5000元区间有效，其他金额无效（单位：分）", required = true)
    @PositiveOrZero
    private int groupBuyFailedAward = 500;
    @ApiModelProperty(value = "拼团成团公益金额（单位：分）", required = true)
    @PositiveOrZero
    private int groupBuyContribution = 300;
    @ApiModelProperty(value = "拼团成团分红金额（单位：分）", required = true)
    @PositiveOrZero
    private int groupBuyBonus = 2000;
    @ApiModelProperty(value = "开售时间")
    private long startSaleTime;

    @Override
    public Product validateAndBuild(ProductService service, HttpServletRequest request, HttpServletResponse response) {
        if (groupBuySuccess <= groupBuySuccessUser) {
            throw new LogicException("成功中签人数必须小于成团人数");
        }
        if (groupBuyFailedAward > 0) {
            if (groupBuyFailedAward < 30 || groupBuyFailedAward > 500000) {
                throw new LogicException("拼团失败奖励金额设置失败，0.3元-5000元区间有效，其他金额无效，微信支付不支持。");
            }
        }
        return super.validateAndBuild(service, request, response);
    }
}
